﻿namespace BlogsAPI
{
    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Xml.Serialization;

        /// <summary>
        /// Некий набор параметров блога
        /// </summary>
        [Serializable]
        [XmlInclude(typeof(PostAttribute))]
        public class PostAttributeCollection : IEnumerable
        {
            private BlogPost _mPOwner;
            private List<PostAttribute> _mPBlogs;

            public PostAttributeCollection()
            {
                _mPBlogs = new List<PostAttribute>();
            }

            /// <summary>
            /// Default constructor.
            /// </summary>
            public PostAttributeCollection(BlogPost bp)
            {
                _mPBlogs = new List<PostAttribute>();
                Owner = bp;
            }


            #region method Add

            /// <summary>
            /// Adds a new blog to the end of the collection.
            /// </summary>
            /// <param name="blog">Mailbox to add.</param>
            public void Add(PostAttribute blog)
            {
                _mPBlogs.Add(blog);

            }

            /// <summary>
            /// Это бесполезная функция, нужна только для сериализации
            /// </summary>
            /// <param name="blog"></param>
            public void Add(Object blog)
            {
                Add((PostAttribute)blog);
            }

            #endregion

            #region method Insert

            /// <summary>
            /// Inserts a new blog into the collection at the specified location.
            /// </summary>
            /// <param name="index">The location in the collection where you want to add the blog.</param>
            /// <param name="blog">Mailbox to add.</param>
            public void Insert(int index, PostAttribute blog)
            {
                _mPBlogs.Insert(index, blog);

            }

            #endregion


            #region method Remove

            /// <summary>
            /// Removes header field at the specified index from the collection.
            /// </summary>
            /// <param name="index">Index of the blog which to remove.</param>
            public void Remove(int index)
            {
                _mPBlogs.RemoveAt(index);

            }

            /// <summary>
            /// Removes specified blog from the collection.
            /// </summary>
            /// <param name="blog">Mailbox to remove.</param>
            public void Remove(PostAttribute blog)
            {
                _mPBlogs.Remove(blog);

            }

            #endregion

            #region method Clear

            /// <summary>
            /// Clears the collection of all bloges.
            /// </summary>
            public void Clear()
            {
                _mPBlogs.Clear();

            }

            #endregion



            #region interface IEnumerator

            /// <summary>
            /// Gets enumerator.
            /// </summary>
            /// <returns></returns>
            public IEnumerator GetEnumerator()
            {
                return _mPBlogs.GetEnumerator();
            }

            #endregion

            #region Properties Implementation

            /// <summary>
            /// Gets blog from specified index.
            /// </summary>
            public PostAttribute this[int index]
            {
                get { return _mPBlogs[index]; }
            }

            /// <summary>
            /// Gets bloges count in the collection.
            /// </summary>
            public int Count
            {
                get { return _mPBlogs.Count; }
            }


            /// <summary>
            /// Gets or sets owner of this collection.
            /// </summary>
            internal BlogPost Owner
            {
                get { return _mPOwner; }

                set { _mPOwner = value; }
            }

            #endregion

            #region Method Merge

            /// <summary>
            /// Слияние 
            /// </summary>
            /// <returns></returns>
            public bool MergeWith(PostAttribute ba)
            {
                bool found = false;
                foreach (PostAttribute newone in this)
                {
                    //был найден в атрибутах
                    if (newone.MergeWith(ba))
                    {
                        found = true;
                        break;
                    }
                }
                return found;
            }

            #endregion


        }
}
